<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class nominasi_model extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 30;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'A.TANGGAL';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'desc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$nama = isset($_POST['NAMA_KPS']) ? strval($_POST['NAMA_KPS']) : '';	
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('A.*');
			$this->db->select('B.NAMA_KPS');
			$this->db->from('T_NOMINASI_KKKS A');
			$this->db->join('R_KPS B', 'B.ID_KPS = A.ID_KPS');
			
			$where = "upper(B.NAMA_KPS) like upper('%$nama%') ";
			$this->db->where($where);
			
		$this->db->order_by($sort, $order);
		
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
		
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function getdataedit($id){
		$this->db->flush_cache();
		$this->db->select('*');
		//$this->db->select();
		$this->db->from('T_NOMINASI_KKKS');
		$this->db->where('ID', $id);
		
		return $this->db->get();
	}
	
	function EXIST_T_NOMINASI_KKKS($data){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('T_NOMINASI_KKKS');
		$this->db->where('ID_KPS', $data['ID_KPS']);
		$this->db->where('TAHUN', $data['TAHUN']);
		$this->db->where('BULAN', $data['BULAN']);
		$this->db->where('TANGGAL', FormatDateToOracle($data['TANGGAL']), false);
		
		return ($this->db->get()->num_rows()>0);
		
	}
	
	function EXIST_EDIT_T_NOMINASI_KKKS($data){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('T_NOMINASI_KKKS');
		$this->db->where('ID !=', $data['ID']);
		$this->db->where('ID_KPS', $data['ID_KPS']);
		$this->db->where('TAHUN', $data['TAHUN']);
		$this->db->where('BULAN', $data['BULAN']);
		$this->db->where('TANGGAL', FormatDateToOracle($data['TANGGAL']), false);
		
		return ($this->db->get()->num_rows()>0);
		
	}
	
	/* function EXIST_T_REALISASI_REVENUE($data){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('T_REALISASI_REVENUE');
		$this->db->where('ID_KPS', $data['ID_KPS']);
		$this->db->where('TAHUN', $data['TAHUN']);
		$this->db->where('BULAN', $data['BULAN']);
		
		return ($this->db->get()->num_rows()>0);
		
	} */
	function EXIST_LQ_LEGAL_TASK($data){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('LQ_LEGAL_TASK');
		$this->db->where('ID_KPS', $data['ID_KPS']);
		$this->db->where('TAHUN', $data['TAHUN']);
		$this->db->where('BULAN', $data['BULAN']);
		$this->db->where('STATUS_PROSES >',0);
		
		return ($this->db->get()->num_rows()>0);
		
	}
	function InsertOnDb($data){
		$this->db->flush_cache();
		$this->db->set('ID_KPS', 	$data['ID_KPS']);
		$this->db->set('TAHUN', 	$data['TAHUN']);
		$this->db->set('BULAN', 	$data['BULAN']);
		$this->db->set('TANGGAL', 	FormatDateToOracle($data['TANGGAL']), false);
		$this->db->set('NOMINASI', 	$data['NOMINASI']);
		$this->db->set('LOCKED', '0');
		//Ambil user id dari model auth (dengan kuid dari cookie)
		$this->db->set('I_USER', $this->auth_model->getUID());
		// $this->db->set('I_DATE', date('Y-m-d H:i:s'));
		// $this->db->set('', $data['']);
		// $this->db->set('', $data['']);
		
		return $this->db->insert('T_NOMINASI_KKKS');
		
	}
	
	function UpdateOnDb($data){
		$this->db->flush_cache();
		//$this->db->set('ID_KPS', 	$data['ID_KPS']);
		$this->db->set('TAHUN', 	$data['TAHUN']);
		$this->db->set('BULAN', 	$data['BULAN']);
		$this->db->set('TANGGAL', 	FormatDateToOracle($data['TANGGAL']), false);
		$this->db->set('NOMINASI', 	$data['NOMINASI']);
		//$this->db->set('LOCKED', '0');
		$this->db->set('U_USER', $this->session->userdata('USERID'));
		//$this->db->set('I_DATE', date('Y-m-d H:i:s'));
		// $this->db->set('', $data['']);
		// $this->db->set('', $data['']);
		
		$this->db->where('ID', 	$data['ID']);
		
		return $this->db->update('T_NOMINASI_KKKS');
		
	}
	
	 function DeleteOnDb($kode){
		 //query delet data di db
                $this->db->flush_cache();
                $this->db->where('ID', $kode);
               // $this->db->where('LOCKED =', '0');
                $result = $this->db->delete('T_NOMINASI_KKKS');
               
                if($result) {
                        return TRUE;
                }else {
                        return FALSE;
                }
        }
}

?>